package com.erification.web.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.erification.web.dao.AddressDao;
import com.erification.web.model.Address;
@Transactional @Repository("addressDao")
public class AddressDaoImpl implements AddressDao {
	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public void save(Address address) {
		sessionFactory.getCurrentSession().save(address);
	}

	@Override
	public void update(Address address) {
		sessionFactory.getCurrentSession().update(address);
	}

	@Override
	public void delete(long id) {
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().get(Address.class, id));
	}

	@Override @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Address> getAll(long id) {
		String queryString = "from Address where user_id=:user_id";
		Query query = sessionFactory.getCurrentSession().createQuery(queryString);
		query.setLong("user_id", id);
		return query.list();
	}

	@Override @Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Address getAddressById(long id) {
		return (Address) sessionFactory.getCurrentSession().get(Address.class, id);
	}

}
